//
//  MyCollectionViewController.m
//  Boom
//
//  Created by 云鹏 赵 on 15/3/11.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MyCollectionViewController.h"

@interface MyCollectionViewController ()

@end

@implementation MyCollectionViewController
- (void)dealloc
{
    [_urlStr release];
    [_dataArray release];
    [_myCollectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =  [UIColor whiteColor];
    
    if (self.type == 1) {
        self.title = @"最新资讯";
    }
    if (self.type == 2) {
        self.title = @"电台";
    }
    if (self.type == 3) {
        self.title = @"音乐";
    }
    if (self.type == 4) {
        self.title = @"情感生活";
    }
    
    self.dataArray = [NSMutableArray array];
//    [self afnGetAllModelData];
    
    // 创建collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 150);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) collectionViewLayout:flowLayout];
    self.myCollectionView.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    [self.myCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [_myCollectionView release];

    self.isUpLoading = NO;
    
    [self addHeader];
    [self addFooter];

}

// 下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block MyCollectionViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.myCollectionView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = NO;//标记为下拉操作
        [vc afnGetAllModelData:1];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myCollectionView headerBeginRefreshing];
    
}
// 上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block MyCollectionViewController *vc = self;
    
    
    // 添加上拉刷新尾部控件
    [self.myCollectionView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = YES;//标记为上拉操作
        [vc afnGetAllModelData:self.nextPage];//请求更多数据
        
    }];
}


// collectionview协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    AllModel *allModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.allModel = allModel;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllModel *allModel = [self.dataArray objectAtIndex:indexPath.row];
    TableViewController *tableVC = [[TableViewController alloc] init];
    tableVC.id = allModel.id;
    [self.navigationController pushViewController:tableVC animated:YES];
    [tableVC release];
    
}

// 获取数据方法
- (void)afnGetAllModelData:(NSInteger) page
{
    if (self.type == 1) {
        self.urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=news&condition=hot&device=iPhone&page=%d&per_page=20&status=0&tag_name=", page];
    }
    if (self.type == 2) {
        self.urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=radio&condition=hot&device=iPhone&page=%d&per_page=20&status=0&tag_name=", page];
    }
    if (self.type == 3) {
        self.urlStr = [NSString stringWithFormat: @"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=iPhone&page=%d&per_page=20&status=0&tag_name=", page];
    }
    if (self.type == 4) {
        self.urlStr = [NSString stringWithFormat: @"http://mobile.ximalaya.com/m/explore_album_list?category_name=emotion&condition=hot&device=iPhone&page=%d&per_page=20&status=0&tag_name=", page];
    }

    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = self.urlStr;
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        // 记录下一页
        self.nextPage = page + 1;
        
        if (self.isUpLoading == NO) {
            [self.dataArray removeAllObjects];
        }
        
        
        NSMutableDictionary *dictonary = responseObject;
        NSMutableArray *array = [dictonary objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            AllModel *allModel = [[AllModel alloc] init];
            [allModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:allModel];
            [allModel release];
        }
        [self.myCollectionView footerEndRefreshing];//结束底部刷新效果
        [self.myCollectionView headerEndRefreshing];//结束头部刷新效果
        [self.myCollectionView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
